package com.tepth.remote.control.delegate.home;

import com.tepth.remote.control.base.bean.SubordinateCount;
import com.tepth.remote.control.base.bean.SubordinateGps;
import com.tepth.remote.control.base.config.ApiDictionary;
import com.tepth.remote.core.net.CommonParams;
import com.tepth.remote.core.net.callback.IError;
import com.tepth.remote.core.net.callback.ISoapSuccess;
import com.tepth.remote.core.net.callback.ISuccess;
import com.tepth.remote.core.utils.object.CharacterUtils;

import org.jetbrains.annotations.NotNull;
import org.ksoap2.serialization.SoapObject;

import java.util.ArrayList;
import java.util.WeakHashMap;

/**
 * Description:
 *
 * @author Hequn.Lee
 * @date 2018/11/18
 */
class HomePresenter extends HomeContract.AbstractPresenter {

    private final HomeContract.View mView;
    private final HomeContract.Model mModel;

    HomePresenter(@NotNull HomeContract.View view) {
        mView = view;
        mModel = new HomeModel();
    }

    @Override
    void getSubordinateNumber(String uid) {
        mView.showLoading("正在加载");
        final WeakHashMap<String, Object> params = new WeakHashMap<>();
        params.put("sessionid", "");
        params.put("uid", uid);
        mModel.getSubordinate(params,
                ApiDictionary.API_GET_SUBORDINATE_NUMBER,
                ApiDictionary.SOAP_NAME_SPACE,
                new ISoapSuccess() {
                    @Override
                    public void onSuccess(SoapObject object) {
                        mView.hideLoading();
                        final int code = Integer.parseInt(object.getProperty("Code").toString());
                        if (code == CommonParams.SUCCESS_CODE_NORMAL) {
                            final SoapObject value = (SoapObject) object
                                    .getProperty("Value");
                            mView.getSubordinateNumberSuccess(SubordinateCount
                                    .getInstance()
                                    .setTotal(value.getPropertySafelyAsString("Total"))
                                    .setOnLine(value.getPropertySafelyAsString("OnLine"))
                                    .setOnException(value.getPropertySafelyAsString("OnException"))
                                    .setOutLine(value.getPropertySafelyAsString("OutLine"))
                                    .setOutToday(value.getPropertySafelyAsString("OutToday"))
                                    .setOther(value.getPropertySafelyAsString("Other")));
                        } else {
                            mView.getSubordinateNumberFailed(code, "数据错误");
                        }
                    }
                }, new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        mView.hideLoading();
                        mView.getSubordinateNumberFailed(code, msg);
                    }
                });
    }

    @Override
    void getSubordinateGpsInfo(String uid) {
        mView.showLoading("正在加载");
        final WeakHashMap<String, Object> params = new WeakHashMap<>();
        params.put("sessionid", "");
        params.put("uid", uid);
        mModel.getSubordinate(params,
                ApiDictionary.API_GET_SUBORDINATE_GPS,
                ApiDictionary.SOAP_NAME_SPACE,
                new ISoapSuccess() {
                    @Override
                    public void onSuccess(SoapObject object) {
                        mView.hideLoading();
                        final int code = Integer.parseInt(object.getProperty("Code").toString());
                        if (code == CommonParams.SUCCESS_CODE_NORMAL) {
                            final SoapObject value = (SoapObject) object
                                    .getProperty("Value");
                            final int len = value.getPropertyCount();
                            ArrayList<SubordinateGps> subordinates = new ArrayList<>();
                            for (int i = 0; i < len; i++) {
                                final SoapObject person = (SoapObject) value
                                        .getProperty(i);
                                if (person != null) {
                                    subordinates.add(new SubordinateGps()
                                            .setUid(CharacterUtils.string2Integer(person.getPropertySafelyAsString("Uid")))
                                            .setUserName(person.getPropertySafelyAsString("UserName"))
                                            .setLatitude(CharacterUtils.string2Double(person.getPropertySafelyAsString("Latitude")))
                                            .setLongitude(CharacterUtils.string2Double(person.getPropertySafelyAsString("Longitude")))
                                            .setBearin(CharacterUtils.string2Double(person.getPropertySafelyAsString("Bearin")))
                                            .setGpsSource(person.getPropertySafelyAsString("GpsSource"))
                                            .setSpeed(CharacterUtils.string2Double(person.getPropertySafelyAsString("Speed")))
                                            .setAccuracy(CharacterUtils.string2Double(person.getPropertySafelyAsString("Altitude")))
                                            .setAltitude(CharacterUtils.string2Double(person.getPropertySafelyAsString("Altitude")))
                                            .setCurrentDate(person.getPropertySafelyAsString("CurrentDate"))
                                            .setGpsDate(person.getPropertySafelyAsString("GpsDate"))
                                            .setUserState(CharacterUtils.string2Integer(person.getPropertySafelyAsString("UserState"))));
                                }
                            }
                            mView.getSubordinateGpsSuccess(subordinates);
                        } else {
                            mView.getSubordinateGpsFailed(code, "数据错误");
                        }
                    }
                }, new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        mView.hideLoading();
                        mView.getSubordinateGpsFailed(code, msg);
                    }
                });
    }

    @Override
    void detectionVersion() {
        mModel.getVersion(ApiDictionary.API_CHECK_VERSION, new ISuccess() {
                    @Override
                    public void onSuccess(Object value) {
                        mView.detectionSuccess((String) value);
                    }
                },
                new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        mView.detectionFailed(code, msg);
                    }
                });
    }
}
